module.exports = {
  apiSuccess(data, msg = "ok", status = 200) {
    this.body = { data, msg, status };
    this.status = status;
  },
  apiFail(data, msg = "fail", status = 400) {
    this.body = { data, msg, status };
    this.status = status;
  },
  // 生成token
  getToken(value) {
    return this.app.jwt.sign(value, this.app.config.jwt.secret);
  },
  /**
   * 分页
   * @param {*} model 被查的表
   * @param {*} where 条件是什么
   * @param {*} options 关联的表是哪个
   * @returns 
   */
  async page(model, where = {}, options = {}) {
    let page = this.query.page ? parseInt(this.query.page) : 1;
    let limit = this.query.limit ? parseInt(this.query.limit) : 10;
    let offset = (page - 1) * limit;

    if (!options.order) {
        options.order = [
            ['id', 'DESC']
        ];
    }

    return await model.findAndCountAll({
        where,
        offset,
        limit,
        ...options
    });
}
};
